<template>
  <div>
    <t-modal
      width="800px"
      title="详情"
      :visible="visible"
      @cancel="closeModal()"
      :footer="null"
      wrapClassName="addModal-container"
      forceRender
      :confirmLoading="submitLoading"
    >
      <div class="addModal-body-warp">
        <a-form-model
          ref="ruleForm"
          :model="form"
          :rules="rules"
          :label-col="labelCol"
          :wrapper-col="wrapperCol"
        >
          <!-- 项目名称 -->
          <a-form-model-item ref="proName" label="项目名称">
            <a-input :value="row.proName" disabled />
          </a-form-model-item>
          <!-- 合同名称 -->
          <a-form-model-item ref="contractName" label="合同名称">
            <a-input :value="row.contractName" disabled />
          </a-form-model-item>
          <!-- 合同类型 -->
          <a-form-model-item ref="contractTypeDesc" label="合同类型">
            <a-input :value="row.contractTypeDesc" disabled />
          </a-form-model-item>
          <!-- 合同文件 -->
          <a-form-model-item ref="planInvestment" label="合同文件">
            <a-input disabled v-if="otherFiles.length == 0" />
            <bjUploadFile
              v-else
              :viewMode="true"
              :count="1"
              v-model="otherFiles"
              @preview="viewFile($event)"
              @download="downloadFile($event)"
              :replaceFields="{
                fileName: 'fileName',
                fileSize: 'fileSize',
                fileType: 'fileType',
              }"
            ></bjUploadFile>
          </a-form-model-item>
          <!-- 合同备案表 -->
          <a-form-model-item ref="planInvestment" label="合同备案表">
            <a-input disabled v-if="filingFormFiles.length == 0" />
            <bjUploadFile
              v-else
              :viewMode="true"
              :count="1"
              v-model="filingFormFiles"
              @preview="viewFile($event)"
              @download="downloadFile($event)"
              :replaceFields="{
                fileName: 'fileName',
                fileSize: 'fileSize',
                fileType: 'fileType',
              }"
            ></bjUploadFile>
          </a-form-model-item>
          <!-- 归档时间 -->
          <a-form-model-item ref="planInvestment" label="归档时间">
            <a-input :value="row.modifyTime" disabled />
          </a-form-model-item>
        </a-form-model>
        <!-- 附件预览 -->
        <previewModal
          ref="previewModal"
          :visible.sync="previewModal.visible"
          :loading="previewModal.loading"
        ></previewModal>
      </div>
    </t-modal>
  </div>
</template>
<script>
import { mapGetters } from "vuex";
import { getToken, downloadDfs } from "@/utils/TopVUI";
import bjUploadFile from "@/components/extendCustom/bj-upload-file";
//单文件预览
import previewModal from "@/pages/document/minio/previewModal";
export default {
  name: "addModal",
  props: {
    visible: {
      type: Boolean,
      default: false,
    },
    row: {
      type: Object,
      default: () => {
        return {};
      },
    },
  },
  components: {
    bjUploadFile,
    previewModal,
  },
  data() {
    return {
      otherFiles: [],
      filingFormFiles: [],
      labelCol: { span: 4 },
      wrapperCol: { span: 18 },
      submitLoading: false,
      form: {
        //计划时间
        month: "",
        //计划投资金额
        planInvestment: "",
      },
      rules: {
        month: [
          {
            required: true,
            message: "请选择计划时间",
            trigger: "change",
          },
        ],
        planInvestment: [
          {
            required: true,
            message: "请输入计划投资金额",
            trigger: "blur",
          },
        ],
      },
      //单文件预览
      previewModal: {
        visible: false,
        loading: false,
      },
    };
  },
  computed: {
    ...mapGetters(["configData", "userInfo"]),
    kkFileView() {
      let index = this.configData.findIndex(
        (item) => item.code === "kkFileView"
      );
      if (index >= 0) {
        return this.configData[index].value;
      } else {
        return "http://192.168.1.216:8012/onlinePreview";
      }
    },
  },
  watch: {
    visible: {
      handler(val) {
        if (val) {
          let { otherFiles, filingFormFiles } = this.row;
          try {
            this.otherFiles = JSON.parse(otherFiles || "[]");
          } catch (error) {
            this.otherFiles = [];
          }
          try {
            this.filingFormFiles = JSON.parse(filingFormFiles || "[]");
          } catch (error) {
            this.filingFormFiles = [];
          }
        } else {
          this.resetForm();
        }
      },
    },
  },
  mounted() {},
  methods: {
    //预览
    viewFile(ev) {
      let { url } = ev;
      this.previewModal.loading = true;
      this.$refs.previewModal.type = "file";
      this.$refs.previewModal.fileUrl =
        this.kkFileView +
        "/onlinePreview?url=" +
        encodeURIComponent(Base64.encode(url)) +
        "&watermarkTxt=" +
        //水印内容
        encodeURIComponent("") +
        "&token=" +
        getToken();
      this.previewModal.visible = true;
      this.previewModal.loading = false;
    },

    //下载
    downloadFile(ev) {
      let { url } = ev;
      downloadDfs(url);
    },

    //重置表单
    resetForm() {
      this.form = {
        //计划时间
        month: "",
        //计划投资金额
        planInvestment: "",
      };
      this.submitLoading = false;
      this.$refs.ruleForm.resetFields();
    },

    //关闭
    closeModal() {
      this.resetForm();
      this.$emit("update:visible", false);
    },
  },
};
</script>
<style lang="less" scoped>
/deep/.addModal-container {
  .ant-modal-body {
    .addModal-body-warp {
      .bj-upload-file {
        .choosed-file-warp {
          width: 100%;
          .file-list {
            padding: 0;
            .base-info {
              border-radius: 0;
            }
          }
        }
      }
    }
  }
}
</style>
